﻿using System;
using System.Collections.Generic;
using System.Text;
using Wjtour.Common;
using Wjtour.Domain;
using System.Linq;
using System.Data.Common;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;

namespace Wjtour.BLL
{
    public class traffic_bll : base_dll
    {
        public List<Wjtour.Domain.traffic> GetList(int top)
        {
            string sql = string.Format("select top {0} * from traffic order by id desc", top);
            return GetAll<Wjtour.Domain.traffic>(sql).ToList();
        }

        public PaginationData<Wjtour.Domain.traffic> GetList(string type, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(type))
                where += " and a.cid in (" + type + ")";

            string sql = string.Format(@"select a.* from traffic a
                {0}
                order by a.id desc", where);
            PaginationData<Wjtour.Domain.traffic> data = GetPaginationData<Wjtour.Domain.traffic>(sql, page, pagesize);
            return data;
        }

        public PaginationData<Wjtour.Domain.traffic> GetList(string q, string type, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(q))
                where += " and a.title like '%" + Utility.GetSafeSql(q) + "%' ";
            if (!string.IsNullOrEmpty(type))
                where += " and a.cid in (" + type + ")";

            string sql = string.Format(@"select a.* from traffic a
                {0}
                order by a.id desc", where);
            PaginationData<Wjtour.Domain.traffic> data = GetPaginationData<Wjtour.Domain.traffic>(sql, page, pagesize);
            return data;
        }

        public Wjtour.Domain.traffic Load(int id)
        {
            string sql = "select * from traffic where id=" + id;
            return DbAccess.GetObjects<Wjtour.Domain.traffic>(sql).FirstOrDefault();
        }

        public bool Save(Wjtour.Domain.traffic entity)
        {
            string sql = "";

            DbCommand cmd = DbAccess.CreateCommand();

            if (entity.id > 0)
            {
                sql = @"UPDATE traffic SET 
                cid = @cid,
                title = @title,
                img = @img,
                source = @source,
                content = @content,
                hits = @hits,
                sort = @sort,
                keyword = @keyword,
                [desc] = @desc,
                addtime = @addtime,
                link = @link,
                author = @author,
                zan = @zan
                 WHERE id=@id";
                DbAccess.AddInParameter(cmd, "@id", DbType.Int32, entity.id);

            }
            else
            {
                sql = @"INSERT INTO traffic( 
                cid,title,img,source,content,hits,sort,keyword,[desc],addtime,link,author,zan)VALUES(
                @cid,@title,@img,@source,@content,@hits,@sort,@keyword,@desc,@addtime,@link,@author,@zan)";
            }
            DbAccess.AddInParameter(cmd, "@cid", DbType.Int32, entity.cid);
            DbAccess.AddInParameter(cmd, "@title", DbType.String, entity.title);
            DbAccess.AddInParameter(cmd, "@img", DbType.String, entity.img);
            DbAccess.AddInParameter(cmd, "@source", DbType.String, entity.source);
            DbAccess.AddInParameter(cmd, "@content", DbType.String, entity.content);
            DbAccess.AddInParameter(cmd, "@hits", DbType.Int32, entity.hits);
            DbAccess.AddInParameter(cmd, "@sort", DbType.Int32, entity.sort);
            DbAccess.AddInParameter(cmd, "@keyword", DbType.String, entity.keyword);
            DbAccess.AddInParameter(cmd, "@desc", DbType.String, entity.desc);
            DbAccess.AddInParameter(cmd, "@addtime", DbType.DateTime, entity.addtime);
            DbAccess.AddInParameter(cmd, "@link", DbType.String, entity.link);
            DbAccess.AddInParameter(cmd, "@author", DbType.String, entity.author);
            DbAccess.AddInParameter(cmd, "@zan", DbType.Int32, entity.zan);
            cmd.CommandText = sql;
            cmd.CommandType = System.Data.CommandType.Text;
            return DbAccess.ExecuteNonQuery(cmd) > 0;
        }

        public int BatchDelete(string ids)
        {
            string sql = "delete from traffic where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }
    }
}
